#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 1e5 + 10;

int n, k;
char str[N];
bool st[N];

int calc(){
    int res = 0;

    for(int i = 1; i + 1 <= n; i ++){
        if(str[i] == '0' && str[i + 1] == '1'){
            res += 1;
        }else if(str[i] == '0' && str[i + 1] == '0'){
            res += 0;
        }else if(str[i] == '1' && str[i + 1] == '0'){
            res += 10;
        }else{
            res += 11;
        }
    }

    return res;
}

void solve(){
    cin >> n >> k;
    for(int i = 0; i <= n; i ++) st[i] = false;
    cin >> str + 1;

    if(str[n] == '0'){
        int index = -1;

        for(int i = n; i; i --){
            if(str[i] == '1'){
                index = i;
                break;
            }
        }

        if(index != -1 && n - index <= k){
            k -= n - index;
            swap(str[index], str[n]);
        }
    }

    if(str[1] == '0'){
        int index = -1;
        for(int i = 1; i <= n - 1; i ++){
            if(str[i] == '1'){
                index = i;
                break;
            }
        }

        if(index != -1 && index - 1 <= k){
            swap(str[1], str[index]);
        }
    }
    
    cout << calc() << '\n';

}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        solve();
    }


    return 0;
}